<?php

namespace app\api\controller;

use app\services\PaymentService;
use app\services\WithdrawService;
use support\Db;
use support\Request;

class WithdrawController
{
    public function apply(Request $request)
    {
        try {
            Db::beginTransaction();
            PaymentService::checkPayPassword($request->post('pay_password'));
            $data = $request->post();

            if (!isset($data['amount'])) {
                throw new \Exception('提现金额不正确');
            }

            WithdrawService::add($data);
            Db::commit();
            return ok_rsp('成功');
        } catch (\Exception $e) {
            Db::rollBack();
            return err_rsp($e->getMessage());
        }
    }

    public function list(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $status = $request->post('status', 1);
            $data = WithdrawService::list($page, 10, $status);
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }
}